import lejos.nxt.*;
/**
 * A behavior that uses an ultrasonic sensor
 * to make a car avoid objects in front of the car. 
 *  
 * @author  Ole Caprani
 * @version 23.08.08
 */
public class AvoidFront extends Behavior 
{
	private final SensorPort USSENSOR = SensorPort.S2;
    private UltrasonicSensor us ;
    private final int tooCloseThreshold = 20; // cm
    private final int travelDistance = 20; // cm
           
    public AvoidFront( String name, int LCDrow, Behavior b)
    {
    	super(name, LCDrow, b);
        us = new UltrasonicSensor(USSENSOR);   	
    }
   
    public void run() 
    {
        while (true)
        {
        	super.run();
        	
            int distance = us.getDistance();
            while ( distance > tooCloseThreshold )
            {
                distance = us.getDistance();
                drawInt(distance);
            }

            suppress();
            //MotorPort.A.controlMotor(100, 1); // light on
		    
            drawString("YES");
            //stopCar();
            goTo(0,0);
            /*rotate(90);
            travel(20);*/

            drawString("   ");		    
            //MotorPort.A.controlMotor(0, 1); // light off
            release();
       }
    }
}


